package com.example.testranking;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;



import android.R.string;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	private TextView selection;
	private ArrayList<String> item = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		selection = (TextView)findViewById(R.id.selection);
		MyThread myThread = (MyThread) new MyThread().execute((Void) null);
		
		try {
			item = myThread.get();
		} catch (Exception e) {
			// TODO: handle exception
		}

		setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, item));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		selection.setText(item.get(position).toString());
	}
	
	public static InputStream getInputStreamFromUrl(String url){
		Log.i("getInputStreamFromUrl", "getInputStreamFromUrl start");
		InputStream contentStream = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(new HttpGet(url));
			
			contentStream = response.getEntity().getContent();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.i("getInputStreamFromUrl", "getInputStreamFromUrl end");
		return contentStream;
	}

	
	public class MyThread extends AsyncTask<Void, Void, ArrayList<String>>{
//		@Override
//		protected Void doInBackground(Void... params) {
//			// TODO Auto-generated method stub
//			
//			try {
//				String rtn = getStringFromUrl("http://115.71.238.29:8080/TestWeb/testJson.do");
//				Log.i("rtn", rtn);
//				
//				JSONArray jsonArray = new JSONArray(rtn);
//				for(int i=0;i<=jsonArray.length();i++){
//					item.add(jsonArray.getJSONObject(i).getString("productName"));
//				}
//				
//			} catch (Throwable t) {
//				// TODO: handle exception
//				
//			}
//			
//			return null;
//		}
		
		@Override
		protected ArrayList<String> doInBackground(Void... params) {
			ArrayList<String> item = new ArrayList<String>();
			
			try {
				String rtn = getStringFromUrl("http://115.71.238.29:8080/TestWeb/testJson.do");
				Log.i("rtn", rtn);
				
				
				JSONArray jsonArray = new JSONArray(rtn);
				for(int i=0;i<=jsonArray.length();i++){
					item.add(jsonArray.getJSONObject(i).getString("productName"));
				}
				
			} catch (Throwable t) {
				// TODO: handle exception
				
			}
			
			return item;
		}
		
		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}
		
		public String getStringFromUrl(String url) throws UnsupportedEncodingException{
			BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url), "UTF-8"), 8);
			StringBuffer sb = new StringBuffer();
			try {
				String line = null;

				while((line = br.readLine()) != null){
					sb.append(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return sb.toString();
		}

	}
	
}


